﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

public partial class Admin_Asp_edit_model : System.Web.UI.Page
{    
    protected const string m_itemIDRequestName = "id";

    protected List<ModelItem> m_items = new List<ModelItem>();

    protected ModelItem m_selectedItem = null;
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!LoginHandler.IsLoggedIn(Session))
        {
            Response.Redirect(Paths.admin_index);
        }

        m_items = ModelItemHandler.GetModelItems();

        //LoadItems();

        if (GetSelectedItem() )
        {
            x_index_multiview.SetActiveView(x_editSelected);

            Admin_Asp_UserControls_ModelEditHandler f_control = Page.LoadControl(Paths.admin_uc_modelHandler) as Admin_Asp_UserControls_ModelEditHandler;                  
            f_control.LoadItem(m_selectedItem, m_items);
            
            x_modelhandler.Controls.Add(f_control);
        }
        else
        {
            Session.Remove(Strings.Session_Name);
            Session.Remove(Strings.Session_Thumbnail);
            Session.Remove(Strings.Session_TextureChanges);
            Session.Remove(Strings.Session_Shaders);
            
            x_index_multiview.SetActiveView(x_modellist);
        }
    }
    /*
    private void LoadItems()
    {       
                
    }
    */
    private bool GetSelectedItem()
    {
        try
        {
            if (Request.QueryString[m_itemIDRequestName] != null)
            {
                int f_itemID = Convert.ToInt32(Request.QueryString[m_itemIDRequestName]);

                if (f_itemID >= 0 && f_itemID < m_items.Count)
                {
                    m_selectedItem = m_items[f_itemID];
                    return true;
                }

            }
        }
        catch
        {
            m_selectedItem = null;
        }

        return false;
    }    
}